<script setup lang="ts">
// 使用消息提示框对请求的 message 做统一处理
import { httpEvent } from "../requests";
import { useRouter } from "vue-router";
import useLoadingBar from "./useLoadingBar";

const notification = useNotification();
const router = useRouter();
const loadingBar = useLoadingBar();

httpEvent.on("onRequest", () => {
  // 开始显示加载条
  loadingBar?.start();
});

httpEvent.on("onResponse", (response) => {
  // 结束加载条
  loadingBar?.finish();
  //
  const { data } = response;
  // 对请求返回消息做统一处理
  if (data.message) {
    notification[data.success ? "success" : "error"]({
      content: data.message,
      duration: 1500,
    });
  }
});

httpEvent.on("onError", (param) => {
  //
  loadingBar?.error();
  // 统一处理用户登陆过期
  if (param.status == 401) {
    // 直接跳转登录界面
    return router.push({ name: "login" });
  }
  // 其他错误进行提示
  if (param.message)
    notification.error({ content: param.message, duration: 1500 });
});
</script>
<template></template>
